﻿using System;
using System.Collections.Generic;
using Infrastructure;
using SubsDownloader.Core.Entities;

namespace SubsDownloader.Core.AppSettings
{
    public class SubtitlesSettings : SettingsBase
    {
        /// <summary>
        /// indicate if use the folder name when scaning a movie
        /// </summary>
        public bool PrefreFolderName { get; set; }

        /// <summary>
        /// number of threads that download subtitles
        /// </summary>
        public int WorkerThreads { get; set; }

        /// <summary>
        /// determin the risk of the subtitles in case that there is no exact match
        /// </summary>
        public int DownloadRisk { get; set; }

        /// <summary>
        /// How long (in seconds) the process should wait the user to select a match.
        /// after that the process will continue with no results
        /// </summary>
        public int WaitForUserToSelect { get; set; }

        private List<string> _subtitlesTypes;

        /// <summary>
        /// Suported subtitles extentions, such as .sub, .idx, etc...
        /// </summary>
        public List<string> SubtitlesTypes
        {
            get
            {             
                return _subtitlesTypes;
            }
            set { _subtitlesTypes = value; }
        }

        public List<SubsProvider> SubsProviders { get; set; }

        public List<Expression> Expressions { get; set; }

        public override void SetDefaults ()
        {
            PrefreFolderName = true;
            WorkerThreads = 3;
            DownloadRisk = 1;
            SubtitlesTypes = new List<string> { ".srt", ".sub", ".idx" }; 
            WaitForUserToSelect = Convert.ToInt32( TimeSpan.FromMinutes( 30 ).TotalMilliseconds );
            Expressions = new List<Expression>();
            Expressions.Add( new Expression { Index = 1, Enabled = true, Regex = @"^(?:.*\\)?(?<series>[^\\]+?)[ _.\-\[]+(?:[s]?(?<season>\d+)[ _.\-\[\]]*[ex](?<episode>\d+)|(?:\#|\-\s)(?<season>\d+)\.(?<episode>\d+))(?:[ _.+-]+(?:[s]?\k<season>[ _.\-\[\]]*[ex](?<episode2>\d+)|(?:\#|\-\s)\k<season>\.(?<episode2>\d+))|(?:[ _.+-]*[ex+-]+(?<episode2>\d+)))*[ _.\-\[\]]*(?<title>(?![^\\].*?(?<!the)[ .(-]sample[ .)-]).*?)\.(?<ext>[^.]*)$" } );
            Expressions.Add( new Expression { Index = 2, Enabled = true, Regex = @"^.*?\\?(?<series>[^\\$]+?)(?:s(?<season>[0-3]?\d)\s?ep?(?<episode>\d\d)|(?<season>(?:[0-1]\d|(?<!\d)\d))x?(?<episode>\d\d))(?!\d)(?:[ .-]?(?:s\k<season>e?(?<episode2>\d{2}(?!\d))|\k<season>x?(?<episode2>\d{2}(?!\d))|(?<episode2>\d\d(?!\d))|E(?<episode2>\d\d))|)[ -.]*(?<title>(?![^\\]*?sample)[^\\]*?[^\\]*?)\.(?<ext>[^.]*)$" } );
            Expressions.Add( new Expression { Index = 3, Enabled = true, Regex = @"^(?<series>[^\\$]+)\\[^\\$]*?(?:s(?<season>[0-1]?\d)ep?(?<episode>\d\d)|(?<season>(?:[0-1]\d|(?<!\d)\d))x?(?<episode>\d\d))(?!\d)(?:[ .-]?(?:s\k<season>e?(?<episode2>\d{2}(?!\d))|\k<season>x?(?<episode2>\d{2}(?!\d))|(?<episode2>\d\d(?!\d))|E(?<episode2>\d\d))|)[ -.]*(?<title>(?!.*sample)[^\\]*?[^\\]*?)\.(?<ext>[^.]*)$" } );
            Expressions.Add( new Expression { Index = 4, Enabled = true, Regex = @"(?<series>[^\\\[]*) - \[(?<season>[0-9]{1,2})x(?<episode>[0-9\W]+)\](( |)(-( |)|))(?<title>(?![^\\]*?sample)[^$]*?)\.(?<ext>[^.]*)" } );
            Expressions.Add( new Expression { Index = 5, Enabled = true, Regex = @"(?<series>[^\\$]*) - season (?<season>[0-9]{1,2}) - (?<title>(?![^\\]*?sample)[^$]*?)\.(?<ext>[^.]*)" } );
            Expressions.Add( new Expression { Index = 6, Enabled = true, Regex = @"(^.*?\\?(?<series>[^\\$]+?)[ .-]+(?<firstaired>\d{2,4}[.-]\d{2}[.-]\d{2,4})[ .-]*(?<title>(?![^\\]*?(?<!the)[ .(-]sample[ .)-]).*?)\.(?<ext>[^.]*)$)" } );
     
        }
    }
}
